<template>
	<view class="container">
		<!-- 用户信息区域 -->
		<view class="user-info">
			<view class="circle-decoration circle-1"></view>
			<view class="circle-decoration circle-2"></view>
			<view class="circle-decoration circle-3"></view>
			<view class="avatar-container">
				<image :src="avatarUrl" class="avatar"></image>
			</view>
			<view class="user-details">
				<text class="user-name">{{ realName }}</text>
				<text class="user-id">{{ $t('mine.account') }}: {{ userNo }}</text>
				<text class="user-role">{{ $t('mine.role') }}: {{ roleProperty==2?$t('mine.mobileService'):(roleProperty==3?$t('mine.merchantAlliance'):$t('mine.admin')) }}</text>
			</view>
		</view>

		<!-- 功能选项列表 -->
		<view class="options-list">
			<view class="option-item" @click="handleChangeLanguage">
				<image src="/static/language.png" class="option-icon"></image>
				<text class="option-text">{{ $t('mine.changeLanguage') }}</text>
				<text class="language-text">{{ currentLanguageText }}</text>
				<view class="arrow-icon">
					<uni-icons type="right" size="18" color="#999"></uni-icons>
				</view>
			</view>
			<view class="option-item" @click="handleBindMechanicCard">
				<image src="/static/remote_start.png" class="option-icon"></image>
				<text class="option-text">{{ $t('mine.bindMechanicCard') }}</text>
				<text class="language-text">{{ machineRepairCard || $t('mine.notBound') }}</text>
				<view class="arrow-icon">
					<uni-icons type="right" size="18" color="#999"></uni-icons>
				</view>
			</view>
			<view class="option-item" @click="handleLogout">
				<image src="/static/loginout.png" class="option-icon"></image>
				<text class="option-text">{{ $t('mine.logout') }}</text>
				<view class="arrow-icon">
					<uni-icons type="right" size="18" color="#999"></uni-icons>
				</view>
			</view>
		</view>
	</view>
	<!-- 语言选择器弹窗 -->
	<language-selector v-if="showLanguageSelector" @close="showLanguageSelector = false"
		@language-change="onLanguageChange"></language-selector>
</template>

<script>
	import languageSelector from '../../components/language-selector/language-selector.vue'
	import getLocales from '../../locale/locales.js'
	import http from '../../utils/request.js'
	export default {
		data() {
			return {
				showLanguageSelector: false,
				currentLanguage: uni.getLocale(), // 初始值为当前语言
				userNo: '', // 用户编号
				avatarUrl: '/static/avatar.png', // 头像URL
				realName: this.$t('mine.defaultUserName'), // 真实姓名
				machineRepairCard: '', // 机修卡号
				roleProperty: 0, //1=管理端 2=手机服务端 3=商盟端
				// 可以在这里定义页面所需的数据
			};
		},
		computed: {
			locales() {
				// 多语言选项，text通过$t函数获取翻译
				return getLocales(this.$t)
			},
			// 根据currentLanguage获取对应的显示文本
			currentLanguageText() {
				const currentLang = this.locales.find(item => item.value === this.currentLanguage);
				return currentLang ? currentLang.text : this.currentLanguage;
			}
		},
		onLoad() {
			// 组件挂载时初始化当前语言
			this.initializeLanguage();
			uni.setNavigationBarTitle({
				title: this.$t('tabbar.mine')
			});
			this.roleProperty = uni.getStorageSync('roleProperty');
			// 获取用户信息
			this.getUserInfo();

		},
		onShow() {
			// 页面显示时检查语言是否发生变化
			const storedLanguage = uni.getStorageSync('currentLanguage');
			const systemLanguage = uni.getLocale();
			const currentLanguage = storedLanguage || systemLanguage || 'en';
			if (currentLanguage !== this.currentLanguage) {
				console.log('检测到语言变化，从', this.currentLanguage, '切换到', currentLanguage);
				this.currentLanguage = currentLanguage;
				// 应用i18n语言设置
				if (this.$i18n) {
					this.$i18n.locale = currentLanguage;
				}
				// 重新加载翻译后的数据
			}

			uni.setNavigationBarTitle({
				title: this.$t('tabbar.mine')
			});
		},
		onPullDownRefresh() {
			console.log('触发下拉刷新');
			// 重新获取用户信息
			this.getUserInfo().finally(() => {
				// 所有数据获取完成后，停止下拉刷新动画
				uni.stopPullDownRefresh();
			});
		},
		methods: {
			initializeLanguage() {
				// 优先从本地存储获取语言设置
				const storedLanguage = uni.getStorageSync('currentLanguage');
				// 如果没有存储的语言，则使用系统语言
				const systemLanguage = uni.getLocale();
				// 设置当前语言，默认使用英语
				this.currentLanguage = storedLanguage || systemLanguage || 'en';
				console.log('初始化语言:', this.currentLanguage);
				// 应用i18n语言设置
				if (this.$i18n) {
					this.$i18n.locale = this.currentLanguage;
				}
			},
			// 处理语言切换事件
			onLanguageChange(language) {
				console.log('当前选择的语言：', language);
				// 更新当前语言状态
				this.currentLanguage = language;
				// 应用i18n语言设置（如果应用使用了i18n）
				if (this.$i18n) {
					this.$i18n.locale = language;
				}
				// 保存语言设置到本地存储
				uni.setStorageSync('currentLanguage', language);
				// 强制更新页面以应用新的语言设置
				this.$forceUpdate();
				// 触发全局事件，通知App.vue更新tabbar文本
				uni.$emit('updateLanguage');
				// 显示语言切换成功提示
				uni.showToast({
					title: this.$t('mine.languageSwitchSuccess'),
					icon: 'none'
				});
				//重新设置当前标题
				uni.setNavigationBarTitle({
					title: this.$t('tabbar.mine')
				});
			},
			// 处理切换语言点击事件
			handleChangeLanguage() {
				this.showLanguageSelector = true;
			},

			// 获取用户信息
			async getUserInfo() {
				try {
					const userInfo = await http.post('/user/getUserInfo', {}, {
						showLoading: false // 不显示加载中，避免频繁切换页面时出现loading
					});

					// 更新用户信息
					this.userNo = userInfo.userNo || '';
					this.avatarUrl = userInfo.avatarUrl || '/static/avatar.png';
					this.realName = userInfo.realName || '张三';
					this.machineRepairCard = userInfo.machineRepairCard || '';

				} catch (error) {
					console.error('获取用户信息失败:', error);
					// 如果获取失败，使用默认值
					this.userNo = '10011';
					this.avatarUrl = '/static/avatar.png';
					this.realName = this.$t('mine.defaultUserName');
					this.machineRepairCard = '';
				}
			},

			// 处理绑定机修卡点击事件
			handleBindMechanicCard() {
				uni.navigateTo({
					url: `/pages/mine/bind-mechanic-card?machineRepairCard=${this.machineRepairCard}`
				});
			},

			// 处理退出登录点击事件
			handleLogout() {
				uni.showModal({
					title: $t('mine.tip'),
					content: $t('mine.confirmLogout'),
					success: (res) => {
						if (res.confirm) {
							// 执行退出登录逻辑
							uni.removeStorageSync('token');
							uni.removeStorageSync('shiftVerificationNo');
							uni.reLaunch({
								url: '/pages/login/login'
							});
						}
					}
				});
			}
		}
	};
</script>

<style scoped>
	.container {
		display: flex;
		flex-direction: column;
		min-height: 100%;
		background-color: #f5f5f5;
	}

	/* 用户信息区域样式 */
	.user-info {
		display: flex;
		align-items: center;
		padding: 30px 16px;
		background: #FF5402;
		color: #ffffff;
		border-radius: 0 0 20px 20px;
		box-shadow: 0 4px 12px rgba(255, 84, 2, 0.2);
		position: relative;
		height: 120px;
		overflow: hidden;
	}

	/* 简单的圆形装饰元素 */
	.circle-decoration {
		position: absolute;
		border-radius: 50%;
		background-color: rgba(255, 255, 255, 0.1);
		z-index: 0;
	}

	/* 第一个圆形 - 右上角大圆形 */
	.circle-1 {
		top: -40px;
		right: -40px;
		width: 120px;
		height: 120px;
	}

	/* 第二个圆形 - 左上角中圆形 */
	.circle-2 {
		top: 10px;
		left: 150px;
		width: 60px;
		height: 60px;
		background-color: rgba(255, 255, 255, 0.08);
	}

	/* 第三个圆形 - 右下角小圆形 */
	.circle-3 {
		bottom: -20px;
		right: 100px;
		width: 80px;
		height: 80px;
		background-color: rgba(255, 255, 255, 0.05);
	}

	.avatar-container {
		margin-right: 16px;
		z-index: 1;
	}

	.avatar {
		width: 80px;
		height: 80px;
		border-radius: 50%;
		border: 3px solid rgba(255, 255, 255, 0.3);
		box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
	}

	.user-details {
		flex: 1;
		z-index: 1;
	}

	.user-name {
		font-size: 22px;
		font-weight: bold;
		display: block;
		margin-bottom: 8px;
	}

	.user-id,
	.user-role {
		font-size: 15px;
		display: block;
		margin-bottom: 4px;
		opacity: 0.9;
	}

	/* 选项列表样式 */
	.options-list {
		margin: 15px;
		background-color: #ffffff;
		border-radius: 12px;
		overflow: hidden;
		box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
	}

	.option-item {
		display: flex;
		align-items: center;
		padding: 0 16px;
		height: 66px;
		border-bottom: 1px solid #f0f0f0;
		transition: all 0.3s ease;
	}

	.option-item:active {
		background-color: #f8f8f8;
	}

	.option-item:last-child {
		border-bottom: none;
	}

	.option-icon {
		width: 24px;
		height: 24px;
		margin-right: 14px;
	}

	.option-text {
		flex: 1;
		font-size: 16px;
		color: #333333;
	}

	.language-text {
		font-size: 14px;
		color: #999999;
		margin-right: 10px;
	}

	.arrow-icon {
		color: #999999;
	}

	.badge {
		min-width: 20px;
		height: 20px;
		line-height: 20px;
		text-align: center;
		border-radius: 10px;
		background-color: #FF5402;
		color: #ffffff;
		font-size: 12px;
		margin-right: 10px;
		padding: 0 6px;
	}
</style>